﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class cancelticket : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();
        private Ticket ticket;

        protected void Page_Load(object sender, EventArgs e)
        {
            initTicket();
        }

        private void initTicket()
        {
            int ticketNumber = Convert.ToInt32(Request.QueryString["ticketnumber"]);

            var tickets = from t in Database.Tickets
                          where t.nummer == ticketNumber
                          select t;

            ticket = tickets.First();
        }

        private bool deleteTicket()
        {
            Boolean returnvalue = false;

            try
            {
                Database.Tickets.DeleteOnSubmit(ticket);
                Database.SubmitChanges();
                returnvalue = true;
            }
            catch (Exception e)
            {

            }    

            return returnvalue;
        }

        private bool createFreeTicket()
        {
            Boolean returnvalue = false;

            try
            {
                BijzondereKaart specialTicket = new BijzondereKaart();
                specialTicket.Type = "Vrijkaart";
                specialTicket.Verloopdatum = DateTime.Now.AddYears(2);

                Database.BijzondereKaarts.InsertOnSubmit(specialTicket);
                Database.SubmitChanges();

                returnvalue = true;
            }
            catch (Exception e)
            {

            }    

            return returnvalue;
        }

        protected void buttonFreeTicket_Click(object sender, EventArgs e)
        {
            bool result = deleteTicket();

            if (result)
            {
                result = createFreeTicket();

                if (result)
                {
                    Server.Transfer("printfreeticket.aspx?enddate=" + DateTime.Now.AddYears(2));
                }            
            }           
        }

        protected void buttonNewShow_Click(object sender, EventArgs e)
        {
            bool result = deleteTicket();

            if (result)
            {
                Server.Transfer("newshow.aspx");
            }  
        }
    }
}